var mailData = require('./mailData')();
var self;
cc.Class({
    extends: cc.Component,

    properties: {
        mail_item: cc.Prefab,

        title: cc.RichText,
        author: cc.RichText,
        can_date: cc.RichText,
        mail_text: cc.Label,


        gift_item: cc.Prefab,


        gift_list: cc.Node,

        mail_list: cc.ScrollView,
        mail_list1: cc.ScrollView,


        is_have:cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        self = this;
        this.content = this.mail_list.content;
        this.giftItem_pool = new cc.NodePool();
        this.mailItemArr = [];
        this.giftItemArr = [];
        this.change_mail(0);
        this.initlist();
        this.node.on('foobar', this.backfun);
        self.id = 0;

    },
    backfun(event) {
        // event.detail
        self.change_mail(event.detail.id);
        event.stopPropagation();
    },
    change_mail(idx) {
        self.id = idx;
        self.mail_list1.content.position = cc.v2(0, 240);
        this.title.string = mailData.mulu_data[idx].title;
        this.author.string = mailData.mulu_data[idx].author;
        this.can_date.string = mailData.mulu_data[idx].can_date;
        this.mail_text.string = mailData.mulu_data[idx].text;

        this.pool_giftitem();
        let _data = mailData.mulu_data[idx].gif_list;
        for (var a = 0; a < _data.length; a++) {
            var item ;
            if (this.giftItem_pool.size() > 0) {
                item = this.giftItem_pool.get();
            } else {

                item = cc.instantiate(this.gift_item);
            }
            this.giftItemArr.push(item);
            item.getComponent('gift_item').init(_data[a]);
            item.parent = this.gift_list;
        }
    },
    pool_giftitem(){
        for (var a = 0; a < this.giftItemArr.length; a++) {
            this.giftItem_pool.put(this.giftItemArr[a]);
            this.giftItemArr.splice(a, 1);
            a--;
        }
    },
    initlist() {
        if (this.mailItemArr.length > 0) {
            for (var a = 0; a < this.mailItemArr.length; a++) {
                this.mailItemArr[a].destroy();
                this.mailItemArr.splice(a, 1);
                a--;
            }
        }
        let listArr = mailData.mulu_data;
        if(listArr.length===0){
            this.is_have.getChildByName("mail_text").active=false;
            this.is_have.getChildByName("mail_none").active=true;

            return;
        }else {
            this.is_have.getChildByName("mail_text").active=true;
            this.is_have.getChildByName("mail_none").active=false;
        }
        for (var a = 0; a < listArr.length; a++) {
            var item = cc.instantiate(this.mail_item);
            item.id=listArr[a].id;
            this.mailItemArr.push(item);
            item.getComponent('mail_item').init(listArr[a]);
            this.content.addChild(item);
        }
    },
    start() {

    },
    back() {
        this.node.active = false;
    },
    delete_mail() {
        for (var a = 0; a < this.mailItemArr.length; a++) {
            if (this.mailItemArr[a].id === self.id) {
                this.mailItemArr[a].destroy();
                this.mailItemArr.splice(a,1);
                break;
            }
        }
        if(this.mailItemArr.length===0){
            this.is_have.getChildByName("mail_none").active=true;
            this.is_have.getChildByName("mail_text").active=false;
        }

    },
    // update (dt) {},
});
